package com.googlecode.klenwell.playndev.core.event;

import org.jbox2d.common.Vec2;

import static playn.core.PlayN.graphics;

import playn.core.Pointer.Event;

public class GameEvent {
    
    public EventType type;
    public Event platformEvent;
    public Vec2 pt;
    public Float x, y;
    public Double time;
    public EventBinding binding;

    public GameEvent(EventType type, Event platformEvent) {
        this.type = type;
        this.platformEvent = platformEvent;
        
        if (type == EventType.POINTERSTART || type == EventType.POINTEREND
                || type == EventType.POINTERDRAG) {
            loadPointerEvent(platformEvent);
        }
    }

    private void loadPointerEvent(Event platformEvent) {
        x = platformEvent.x();
        y = platformEvent.y();
        time = platformEvent.time();
        pt = new Vec2(x, y);
        pt = translatePtToWorld(pt);
    }

    public void addBinding(EventBinding binding) {
        this.binding = binding;
    }
    
    private Vec2 translatePtToWorld(Vec2 pt2) {
        return new Vec2(screenToWorldX(x), screenToWorldY(y));
    }
    
    public static float screenToWorldX(float x) {
        Vec2 screenCenter = getDisplayCenter();
        return x - screenCenter.x;
    }

    public static float screenToWorldY(float y) {
        Vec2 screenCenter = getDisplayCenter();
        return screenCenter.y - y;
    }

    public static Vec2 getDisplayCenter() {
        Vec2 displaySize = new Vec2(graphics().width(), graphics().height());
        return displaySize.mul(0.5f);
    }

}
